{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.调用大模型",
   "id": "76f31cde994afe1c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "response = llm.invoke(\"什么是大模型\")\n",
    "print(response)"
   ],
   "id": "23bb759a3d728960",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2.使用提示词模板",
   "id": "282cb99cce6c4035"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([(\"system\", \"你是世界级的技术文档编写者\"), (\"user\", \"{input}\")])\n",
    "\n",
    "# 把提示词和具体的llm调用合在一起\n",
    "chain = prompt | llm\n",
    "message = chain.invoke({\"input\": \"大模型中的langchain是什么\"})\n",
    "print(message)\n",
    "\n"
   ],
   "id": "2ac80239a7d60722",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3.使用输出解析器",
   "id": "7017d0016664695c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T05:17:00.040031Z",
     "start_time": "2025-09-22T05:16:57.142989Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.output_parsers import JsonOutputParser\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "prompt = ChatPromptTemplate.from_messages([(\"system\", \"你是世界级的技术文档编写者\"), (\"user\", \"{input}\")])\n",
    "\n",
    "# 使用输出解析器\n",
    "output_parser = JsonOutputParser()\n",
    "\n",
    "chain = prompt | llm | output_parser\n",
    "message = chain.invoke({\"input\": \"大模型中的langchain是什么,用JSON格式回复\"})\n",
    "print(message)\n"
   ],
   "id": "3bbf7d7820e185ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'langchain': {'definition': 'LangChain是一个用于构建基于语言模型的应用程序的框架，旨在简化与语言模型的交互。', 'features': ['组件化设计：允许开发者创建和组合不同的模块以构建复杂的应用。', '数据管道：通过与外部数据源集成，增强语言模型的能力。', '可扩展性：支持多种语言模型和工具，包括OpenAI、Hugging Face等。', '内置模板：提供了用于生成文本的通用模板，简化开发流程。'], 'use_cases': ['自动化客服：构建能够理解和回应用户查询的聊天机器人。', '内容生成：自动生成文章、故事等文档。', '个性化推荐：根据用户输入生成个性化的建议。', '数据分析：使用自然语言处理工具分析和解读数据。'], 'installation': {'language': 'Python', 'command': 'pip install langchain'}, 'documentation': 'https://langchain.readthedocs.io/'}}\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
